package com.blackbox.price.controller;

import com.blackbox.price.configuration.VisionResult;
import com.blackbox.price.entity.BbImg;
import com.blackbox.price.mapper.common.VisionService;
import com.blackbox.price.service.CommonService;
import com.blackbox.price.service.ImgService;
import com.blackbox.price.util.FileUploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import sun.misc.BASE64Encoder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author: TWQ
 * @Description:
 * @Date: Created in 下午 23:00 2020/8/18 0024
 * @Modidied By: NO
 */
@Slf4j
@CrossOrigin
@RestController
@Api(tags = "公共管理")
@RequestMapping("common")
public class CommonController {


    @Value("${fileUpload.root}")
    private String fileRoot;

    @Resource
    private CommonService commonService;

    @Resource
    private ImgService imgService;

    @ApiOperation(value = "附件上传")
    @PostMapping("uploadFile")
    public VisionResult uploadFile(HttpServletRequest request, String filePath) {
        try {
            Map<String, String> map = FileUploadUtil.getUtil().uploadFile(request, "file", filePath, new String[]{".rar", ".RAR", ".xls", ".xlsx", "doc", "docx", "zip", "pdf", "pptx", "ppt", "doc", "docx", "jpg", "png", "ZIP", "PTF", "PPTX", "PPT", "DOC", "DOCX", "JPG", "PNG"}, true);
            return VisionResult.oK(map);
        } catch (Exception e) {
            e.printStackTrace();
            return VisionResult.build(VisionResult.ERROR, "");
        }
    }

    @ApiOperation(value = "图片上传")
    @PostMapping("uploadImg")
    public VisionResult uploadImg(HttpServletRequest request, @RequestParam("file")MultipartFile [] files) {
        //System.out.println(files.length);
        try {
            MultipartFile file ;
            /*List<MultipartFile> fileList = multiFileMap.get("uploadFile");*/
            if (files.length > 0) {
                file = files[0];
            } else {
                throw new IllegalArgumentException("没有获取到上传的文件");
            }

            String imgUUID = UUID.randomUUID().toString();
            String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf('.')).toLowerCase();
            String newName = UUID.randomUUID().toString().replaceAll("-", "") + suffix;
            File fj = new File( fileRoot + newName);
            file.transferTo(fj);
            return VisionResult.oK(newName);
        } catch (Exception e) {
            e.printStackTrace();
            return VisionResult.build(VisionResult.ERROR, "");
        }
    }

    private String turn2Base64 (MultipartFile file) {
        String base64Result = "";
        try {
            BASE64Encoder bEncoder=new BASE64Encoder();
            String[] suffixArra=file.getOriginalFilename().split("\\.");
            String preffix="data:image/jpg;base64,".replace("jpg", suffixArra[suffixArra.length - 1]);
            base64Result=preffix + bEncoder.encode(file.getBytes()).replaceAll("[\\s*\t\n\r]", "");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return base64Result;
    }
}
